<?php
require 'facebook.php';
class FacebookLogin extends CWidget
{
	public $postParams=array();
	public $config=array();

	public function run()
    {
    	$app_id = $this->config['app_id'];
    	$app_secret = $this->config['app_secret'];
    	
    	$facebook = new Facebook(array('appId'  => $app_id,'secret' => $app_secret));
    	$userId = $facebook->getUser();
		if ($userId) 
		{ 
      		$userInfo = $facebook->api('/' + $userId);
      		//print_r($userInfo);
      		//exit;
     	} 
     	else 
     	{  ?>
    		<div id="fb-root"></div>
    		<fb:login-button></fb:login-button>
     	<?php 
     	} 
     	?>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId      : '<?php echo $app_id ?>',
          status     : true, 
          cookie     : true,
          xfbml      : true,
          oauth      : true,
        });

        FB.Event.subscribe('auth.login', function(response) {
        	window.location.reload();
            });
      };

      (function(d){
         var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
         js = d.createElement('script'); js.id = id; js.async = true;
         js.src = "//connect.facebook.net/en_US/all.js";
         d.getElementsByTagName('head')[0].appendChild(js);
       }(document));
    </script>
    <?php 
    
    }
}